﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class receivesms : System.Web.UI.Page
{
    const int MobileNoLength = 10;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Logs.Write("OK1");
            //string smsmsg = Request.Params["message"].ToString();
            //string to = Request.Params["to"].ToString();
            //string from = Request.Params["from"].ToString();     
            string smsmsg = Request.QueryString["message"];
            string to = Request.QueryString["to_number"];
            string from = Request.QueryString["from_number"];
            Logs.Write(DateTime.Now.ToString());
            Logs.Write(from);
            Logs.Write(to);
            Logs.Write(smsmsg);
            if (from != null && from.Trim().Length > 0 && smsmsg != null && smsmsg.Trim().Length > 0)
            {
                if (from.Contains('+'))
                {
                    from = from.Remove(from.IndexOf('+'), 1);
                }
                if (from.Length > MobileNoLength)
                {
                    from = from.Substring(from.Length - MobileNoLength, MobileNoLength);
                }
                else if (from.Length < MobileNoLength)
                {
                    Response.Write("Invalid mobile number.");
                    return;
                }
                if (to.Length > MobileNoLength)
                {
                    to = to.Substring(to.Length - MobileNoLength, MobileNoLength);
                }
                SqlHelper.ExecuteNonQuery(GlobalVariable.con, CommandType.Text, "insert into tbl_inbox (MsgTo,MsgFrom,MsgText,MsgDate) values('" + to + "','" + from + "','" + smsmsg + "','" + DateTime.Now + "')");
                //if (smsmsg == "STOP")
                //{
                //    //SqlHelper.ExecuteNonQuery(GlobalVariable.con, CommandType.Text, "delete from tbl_groupcontacts where MobileNumber='" + from + "'");
                //}
            } 
        }
        catch (Exception exp)
        {
            Response.Write(exp.ToString());
        }
    }
}
